<?php

namespace app\api\controller;

use app\api\model\VenueSignup as mVenueSignup;
use app\api\model\Entrance;
use app\api\model\VenueInfo;
use app\common\controller\Api;
use think\captcha\Captcha;

class Venue extends Api
{
    protected $noNeedLogin = ['info','entrance','ListLog'];

    public function info()
    {
        $where['id']=1;
        $data=(new VenueInfo())->where($where)->find();
        $this->success('', $data);
    }
    public function entrance()
    {
        $timese = request()->param('time');
        if(empty($timese)){
            $timese=time();
        }
        $k=strtotime(date('Y-m-d 00:00:00',$timese));
        $e=strtotime(date('Y-m-d 23:59:59',$timese));
        $where_find['date']=['between',[$k,$e]];
        $list=(new Entrance())->order('weigh desc')->select();
         foreach ($list as &$item){
             $time[0]['title']="9:00-10:00";
             $time[0]['state']=1;
             $time[1]['title']="10:00-11:00";
             $time[1]['state']=1;
             $time[2]['title']="13:00-14:00";
             $time[2]['state']=1;
             $time[3]['title']="14:00-16:00";
             $time[3]['state']=1;
//             dump($time);die;
             foreach ($time as $k=>$v){
                 $where_find['time']=$v['title'];
                 $find=(new mVenueSignup())->where($where_find)->find();
                 if(!empty($find)){
                     $time[$k]['state']=0;
                 }
             }
             $item['time']=$time;
         }
        $this->success('',$list);
    }
    public function signUp()
    {
        $param = request()->param();
        $captcha = new Captcha();
        $is_code=$captcha->check($param['code'],'');
        if(!$is_code){
            $this->error('验证码错误');
        }
        //判断时间段是否有人预约
        switch ($param['time']){
            case 1:
                $time="9:00-10:00";
                break;
            case 2:
                $time="10:00-11:00";
                break;
            case 3:
                $time="13:00-14:00";
                break;
            case 4:
                $time="14:00-16:00";
                break;
        }
        $where_find['time']=$time;
        $k=strtotime(date('Y-m-d 00:00:00',$param['date']));
        $e=strtotime(date('Y-m-d 23:59:59',$param['date']));
        $where_find['date']=['between',[$k,$e]];
        $find=(new mVenueSignup())->where($where_find)->find();
        if(!empty($find)){
            $this->error('此时间段已预约');
        }
        $user = $this->auth;
        $user->id=1;
        $data['user_id']=$user->id;
        $data['unit']=$param['unit'];
        $data['name']=$param['name'];
        $data['phone']=$param['phone'];
        $data['num']=$param['num'];
        $data['time']=$time;
        $data['date']=$param['date'];
        $data['createtime']=time();
        (new mVenueSignup())->save($data);
        $this->success('');
    }
    public function ListLog()
    {
        $param = request()->param();
        $user = $this->auth;
        $user->id=1;
        $where['user_id']=$user->id;
        if($param['type']==1){
            $where['date']=['>',time()];
        }else{
            $where['date']=['<',time()];
        }
        $list=(new mVenueSignup())->where($where)->order('id desc')->paginate()->each(function($item,$key){
            return $item;
        });
        $this->success('',$list);
    }
}